package com.mxc.website.util;

import cn.hutool.extra.mail.MailAccount;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author wendongdong
 */
public class SendMessageUtil {
    private static MailAccount account = null;

    private static final ReadWriteLock RWL = new ReentrantReadWriteLock();

    /**
     * # 邮件服务器的SMTP地址，可选，默认为smtp.<发件人邮箱后缀>
     * host = smtp.yeah.net
     * # 邮件服务器的SMTP端口，可选，默认25
     * port = 25
     * # 发件人（必须正确，否则发送失败）
     * from = hutool@yeah.net
     * # 用户名，默认为发件人邮箱前缀
     * user = hutool
     * # 密码（注意，某些邮箱需要为SMTP服务单独设置授权码，详情查看相关帮助）
     * pass = q1w2e3
     * @return
     */
    public static MailAccount getAccount(){
        RWL.readLock().lock();
        try{
            if (null == account) {
                RWL.readLock().unlock();
                RWL.writeLock().lock();
                if(null == account){
                    MailAccount mail =new MailAccount();
                    mail.setHost("smtp.exmail.qq.com");
                    mail.setPort(465);
                    mail.setAuth(true);
                    mail.setFrom("eletilemf@eletile.com");
                    mail.setUser("eletilemf@eletile.com");
                    mail.setPass("Mf202410");
                    mail.setSslEnable(true);
                    account = mail;
                }
                RWL.writeLock().unlock();
            }
            RWL.readLock().lock();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            RWL.readLock().unlock();
        }
        return account;

    }

}
